package com.jie.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.clients.UserAddressClient;
import com.jie.dao.GoodsDao;
import com.jie.dao.OrdersDao;
import com.jie.entity.Goods;
import com.jie.entity.OrderInfo;
import com.jie.entity.Orders;
import com.jie.mapper.OrdersMapper;
import com.jie.service.GoodsService;
import com.jie.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.utility.R;
import com.jie.utility.UserAddress;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jie
 * @since 2024-03-21
 */

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private  OrdersMapper ordersMapper;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private UserAddressClient addressClient;

    @Override
    public List<OrdersDao> allOrders(Orders orders) {
        LambdaQueryWrapper<Orders>  queryWrapper=new LambdaQueryWrapper<>();
        if(orders.getUserId()!=null){
            queryWrapper.eq(Orders::getUserId,orders.getUserId());
            queryWrapper.eq(Orders::getIsDeleted,0);
        }
        if(orders.getAdminId()!=null){
            queryWrapper.eq(Orders::getAdminId,orders.getAdminId());
        }
        List<OrdersDao> ordersDaos=new ArrayList<>();
        List<Orders> orders1 = ordersMapper.selectList(queryWrapper);
        if(orders1.size()<0){
            return ordersDaos;
        }
        for (Orders order:orders1) {
            OrdersDao ordersDao=new OrdersDao();
            GoodsDao goodsDao = goodsService.findidfigure(order.getGoodsId());
                BeanUtils.copyProperties(goodsDao,ordersDao);
                ordersDao.setOrdersId(order.getOrdersId());
                ordersDao.setUserId(order.getUserId());
                ordersDao.setNumber(order.getNumber());
                ordersDao.setTotalPrice(order.getTotalPrice());
                ordersDao.setStatus(order.getStatus());
                UserAddress userAddress = addressClient.findById(order.getAddressId());
                ordersDao.setAddress(userAddress.getAddress());
                ordersDao.setName(userAddress.getName());
                ordersDao.setTel(userAddress.getTel());
                ordersDao.setManyAttrsIndex(order.getManyAttrsIndex());
                ordersDao.setOrderCreateTime(order.getCreateTime());
                ordersDaos.add(ordersDao);
        }
        return ordersDaos;
    }

    @Override
    public R<List<OrdersDao>> getOrders(OrderInfo orderInfo) {
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        Page page=new Page<>(orderInfo.getPagenum(), orderInfo.getPagesize());
        List<OrdersDao> ordersDaos=new ArrayList<>();
        Map<String,Long> map=new HashMap<>();
        queryWrapper.eq(Orders::getAdminId,orderInfo.getAdminId());
        if(orderInfo.getStatus()!=-1){
            queryWrapper.eq(Orders::getStatus,orderInfo.getStatus().toString());
        }
       if(StringUtils.isNotEmpty(orderInfo.getQuery())){
           LambdaQueryWrapper<Goods> wrapper=new LambdaQueryWrapper<>();
           wrapper.like(Goods::getGoodsName,'%'+orderInfo.getQuery()+'%').select(Goods::getGoodsId);
           List<Goods> list = goodsService.list(wrapper);
           if(list.size()<0){
               map.put("total",0L);
               return R.success(ordersDaos,map);
           }
           List<String> ids=new ArrayList<>();
           for (Goods good:list) {
               ids.add(good.getGoodsId());
           }
           queryWrapper.in(Orders::getGoodsId,ids);
       }
       queryWrapper.orderByDesc(Orders::getCreateTime);
        ordersMapper.selectPage(page, queryWrapper);
        map.put("total",page.getTotal());
        if(page.getRecords()!=null){
            for (Object record : page.getRecords()) {
                OrdersDao ordersDao = new OrdersDao();
                Orders orders= JSON.parseObject(JSON.toJSONString(record), Orders.class);
                System.out.println(orders.getStatus());
                GoodsDao goodsDao = goodsService.findidfigure(orders.getGoodsId());
                BeanUtils.copyProperties(goodsDao,ordersDao);
                ordersDao.setOrdersId(orders.getOrdersId());
                ordersDao.setUserId(orders.getUserId());
                ordersDao.setNumber(orders.getNumber());
                ordersDao.setTotalPrice(orders.getTotalPrice());
                ordersDao.setStatus(orders.getStatus());
                UserAddress userAddress = addressClient.findById(orders.getAddressId());
                ordersDao.setAddress(userAddress.getAddress());
                ordersDao.setName(userAddress.getName());
                ordersDao.setTel(userAddress.getTel());
                ordersDao.setManyAttrsIndex(orders.getManyAttrsIndex());
                ordersDao.setOrderCreateTime(orders.getCreateTime());
                ordersDaos.add(ordersDao);
            }
        }
        return R.success(ordersDaos,map);
    }

}
